package jsp.servlet;

import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.entities.Employee;
import jpa.entities.Project;
import jsp.Datetime;
import jsp.classes.Session;

/**
 * @author Remco Haszing
 */
public class UpdateProject extends HttpServlet {

    /**
     * Updates the user's personal data according to the data in the request
     * parameter. This method updates the user's properties and merges them into
     * the database.
     *
     * @param request The request that should be handled.
     * @param response Unused.
     * @throws IOException  
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Session session = (Session) request.getSession(true).getAttribute("user");
            
            int projectId = Integer.parseInt(request.getParameter("projectId"));

            
            Project p = session.findProjectById(projectId);
            String deadline[] = null;            
            
            for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
                String k = entry.getKey();
                String[] v = entry.getValue();
                if (k.equals("projectName")) {
                     p.setTitle(v[0]);
                } else if (k.equals("projectLeader")) {
                    int empId = Integer.parseInt(v[0]);
                    
                   
                    Employee emp = session.findEmployeeById(empId);
                    if(emp != null)
                        p.setProjectLeader(emp);
                } else if (k.equals("projectDate")) {

                    
                    // nog de juiste datum omzetten.
                    GregorianCalendar cal = new GregorianCalendar();
                    
                    
                    p.setStartingDate(cal);
                } else if (k.equals("projectState")) {
                    p.setFinished(true);
                }
                

            }
            
            
            session.getJPAConnection().edit(p);
            response.getWriter().print("true");
        } catch (Exception e) {
            response.getWriter().print("false");
        }
    }
}
